<!doctype html>



  


<html class="theme-next mist use-motion">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>



<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />












  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.4.0" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.0.2" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="数据存储," />








  <link rel="shortcut icon" type="image/x-icon" href="/favicon.ico?v=5.0.2" />






<meta name="description" content="&amp;#x7B80;&amp;#x4ECB;:CoreData &amp;#x662F;&amp;#x6570;&amp;#x636E;&amp;#x6301;&amp;#x4E45;&amp;#x5316;&amp;#x5B58;&amp;#x50A8;&amp;#x7684;&amp;#x6700;&amp;#x4F73;&amp;#x65B9;&amp;#x5F0F;.CoreData &amp;#x662F;&amp;#x57FA;&amp;#x4E8E; sqlite &amp;#x7684;&amp;#x5C01;&amp;#x88C5;, &amp;">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS开发 - 数据存储 CoreData 的使用">
<meta property="og:url" content="http://www.licheng244.com/posts/2016-11/03-2.html">
<meta property="og:site_name" content="LiCheng的博客">
<meta property="og:description" content="&amp;#x7B80;&amp;#x4ECB;:CoreData &amp;#x662F;&amp;#x6570;&amp;#x636E;&amp;#x6301;&amp;#x4E45;&amp;#x5316;&amp;#x5B58;&amp;#x50A8;&amp;#x7684;&amp;#x6700;&amp;#x4F73;&amp;#x65B9;&amp;#x5F0F;.CoreData &amp;#x662F;&amp;#x57FA;&amp;#x4E8E; sqlite &amp;#x7684;&amp;#x5C01;&amp;#x88C5;, &amp;">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1975627-57ef108b72733513.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1975627-036a03dec180ca9f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1975627-0a11757c6d46ff45.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1975627-7b81372c0979bb12.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1975627-543672f4693585b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1975627-d50caf04845c5c6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="http://upload-images.jianshu.io/upload_images/1975627-8c49644ef29bb95f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2017-09-18T08:59:37.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="iOS开发 - 数据存储 CoreData 的使用">
<meta name="twitter:description" content="&amp;#x7B80;&amp;#x4ECB;:CoreData &amp;#x662F;&amp;#x6570;&amp;#x636E;&amp;#x6301;&amp;#x4E45;&amp;#x5316;&amp;#x5B58;&amp;#x50A8;&amp;#x7684;&amp;#x6700;&amp;#x4F73;&amp;#x65B9;&amp;#x5F0F;.CoreData &amp;#x662F;&amp;#x57FA;&amp;#x4E8E; sqlite &amp;#x7684;&amp;#x5C01;&amp;#x88C5;, &amp;">
<meta name="twitter:image" content="http://upload-images.jianshu.io/upload_images/1975627-57ef108b72733513.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Mist',
    sidebar: {"position":"left","display":"post"},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    }
  };
</script>




  <link rel="canonical" href="http://www.licheng244.com/posts/2016-11/03-2.html"/>


  <title> iOS开发 - 数据存储 CoreData 的使用 | LiCheng的博客 </title>
</head>

<body itemscope itemtype="//schema.org/WebPage" lang="zh-Hans">

  










  
  
    
  

  <div class="container one-collumn sidebar-position-left page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="//schema.org/WPHeader">
      <div class="header-inner"><div class="site-meta ">
  

  <div class="custom-logo-site-title">
    <a href="/"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">LiCheng的博客</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle"></p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-lifes">
          <a href="/lifes" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-question-circle"></i> <br />
            
            生活
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>


    </div>
  
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  
  

  
  
  

  <article class="post post-type-normal " itemscope itemtype="//schema.org/Article">

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">
            
            
              
                iOS开发 - 数据存储 CoreData 的使用
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">
            <span class="post-meta-item-icon">
              <i class="fa fa-calendar-o"></i>
            </span>
            <span class="post-meta-item-text">发表于</span>
            <time itemprop="dateCreated" datetime="2016-11-03T00:00:00+08:00" content="2016-11-03">
              2016-11-03
            </time>
          </span>

          
            <span class="post-category" >
              &nbsp; | &nbsp;
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
              
                <span itemprop="about" itemscope itemtype="https://schema.org/Thing">
                  <a href="/categories/iOS开发-高级/" itemprop="url" rel="index">
                    <span itemprop="name">iOS开发__高级</span>
                  </a>
                </span>

                
                

              
            </span>
          

          
            
              <span class="post-comments-count">
                &nbsp; | &nbsp;
                <a href="/posts/2016-11/03-2.html#comments" itemprop="discussionUrl">
                  <span class="post-comments-count ds-thread-count" data-thread-key="posts/2016-11/03-2.html" itemprop="commentsCount"></span>
                </a>
              </span>
            
          


          

          
          
             <span id="/posts/2016-11/03-2.html" class="leancloud_visitors" data-flag-title="iOS开发 - 数据存储 CoreData 的使用">
               &nbsp; | &nbsp;
               <span class="post-meta-item-icon">
                 <i class="fa fa-eye"></i>
               </span>
               <span class="post-meta-item-text">阅读次数 </span>
               <span class="leancloud-visitors-count"></span>
              </span>
          

          
        </div>
      </header>
    


    <div class="post-body" itemprop="articleBody">

      
      

      
        <blockquote>
<p><strong>&#x7B80;&#x4ECB;:</strong><br>CoreData &#x662F;&#x6570;&#x636E;&#x6301;&#x4E45;&#x5316;&#x5B58;&#x50A8;&#x7684;&#x6700;&#x4F73;&#x65B9;&#x5F0F;.<br>CoreData &#x662F;&#x57FA;&#x4E8E; sqlite &#x7684;&#x5C01;&#x88C5;, &#x6570;&#x636E;&#x4FDD;&#x5B58;&#x5230;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x6587;&#x4EF6;&#x4E2D;(&#x8868;).<br>iOS5 &#x4EE5;&#x540E;&#x624D;&#x51FA;&#x73B0;.<br>&#x5B83;&#x63D0;&#x4F9B;&#x4E86;&#x5BF9;&#x8C61;-&#x5173;&#x7CFB;&#x6620;&#x5C04;(ORM)&#x7684;&#x529F;&#x80FD;, &#x65E2;&#x80FD;&#x591F;&#x5C06;oc&#x5BF9;&#x8C61;&#x8F6C;&#x5316;&#x6210;&#x6570;&#x636E;&#x4FDD;&#x5B58;&#x5728;sqlite&#x6587;&#x4EF6;&#x4E2D;, &#x4E5F;&#x80FD;&#x5C06;&#x4FDD;&#x5B58;&#x5728;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x6570;&#x636E;&#x8FD8;&#x539F;&#x6210;oc&#x5BF9;&#x8C61;.<br>&#x6570;&#x636E;&#x64CD;&#x4F5C;&#x671F;&#x95F4;, &#x6211;&#x4EEC;&#x4E0D;&#x9700;&#x8981;&#x7F16;&#x5199;&#x4EFB;&#x4F55;&#x7684;sql&#x8BED;&#x53E5;.<br>&#x6570;&#x636E;&#x6700;&#x7EC8;&#x7684;&#x5B58;&#x50A8;&#x7C7B;&#x578B;&#x53EF;&#x4EE5;&#x662F;&#xFF1A;SQLite&#x6570;&#x636E;&#x5E93;&#xFF0C;XML&#xFF0C;&#x4E8C;&#x8FDB;&#x5236;&#xFF0C;&#x5185;&#x5B58;&#x91CC;&#xFF0C;&#x6216;&#x81EA;&#x5B9A;&#x4E49;&#x6570;&#x636E;&#x7C7B;&#x578B;.<br>&#x597D;&#x5904;&#xFF1A;&#x80FD;&#x591F;&#x5408;&#x7406;&#x7BA1;&#x7406;&#x5185;&#x5B58;,&#x907F;&#x514D;&#x4F7F;&#x7528;sql&#x7684;&#x9EBB;&#x70E6;, &#x66F4;&#x9AD8;&#x6548;. &#x4E0D;&#x9700;&#x8981;&#x6211;&#x4EEC;&#x624B;&#x52A8;&#x53BB;&#x521B;&#x5EFA;model&#x7C7B;, &#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x4E00;&#x4E9B;&#x64CD;&#x4F5C;, &#x5236;&#x52A8;&#x751F;&#x6210;.</p>
</blockquote>
<a id="more"></a>
<hr>
<h3 id="&#x5DE5;&#x4F5C;&#x539F;&#x7406;"><a href="#&#x5DE5;&#x4F5C;&#x539F;&#x7406;" class="headerlink" title="&#x5DE5;&#x4F5C;&#x539F;&#x7406;"></a>&#x5DE5;&#x4F5C;&#x539F;&#x7406;</h3><ol>
<li><strong>NSManagedObjectContext</strong> &#x4E34;&#x65F6;&#x6570;&#x636E;&#x5E93; &#x5411;<strong>NSPersistentStoreCoordinator</strong> &#x6301;&#x4E45;&#x5316;&#x5B58;&#x50A8;&#x52A9;&#x7406; &#x53D1;&#x9001;&#x4E00;&#x4E2A;key(model&#x540D;&#x5B57;)</li>
<li><strong>NSPersistentStoreCoordinator</strong> &#x901A;&#x8FC7;&#x8FD9;&#x4E2A;key &#x5728; <strong>NSManagedObjectModel</strong> &#x6570;&#x636E;&#x6A21;&#x578B;&#x4E2D; &#x627E;&#x5230;&#x8FD9;&#x4E2A;model &#x5BF9;&#x5E94;&#x7684; &#x8868;</li>
<li><strong>NSManagedObjectModel</strong> &#x5C06;&#x8FD9;&#x4E2A;&#x8868;&#x540D;  &#x8FD4;&#x56DE;&#x7ED9; <strong>NSPersistentStoreCoordinator</strong></li>
<li><strong>NSPersistentStoreCoordinator</strong> &#x901A;&#x8FC7; &#x8868;&#x540D; &#x627E;&#x5230; &#x7ED9;&#x8868;&#x7684; file &#x8DEF;&#x5F84;</li>
<li><strong>NSPersistentStoreCoordinator</strong> &#x5C06;&#x8FD9;&#x4E2A;&#x8DEF;&#x5F84; &#x8FD4;&#x56DE;&#x7ED9; <strong>NSManagedObjectContext</strong></li>
<li><strong>NSManagedObjectContext</strong> &#x5BF9; &#x6570;&#x636E;&#x8FDB;&#x884C; &#x5904;&#x7406;(&#x589E;, &#x5220; , &#x8BE5;, &#x67E5;)</li>
<li><strong>NSManagedObjectContext</strong> &#x4FDD;&#x5B58;&#x6570;&#x636E; save</li>
</ol>
<h3 id="&#x4F7F;&#x7528;&#x6B65;&#x9AA4;"><a href="#&#x4F7F;&#x7528;&#x6B65;&#x9AA4;" class="headerlink" title="&#x4F7F;&#x7528;&#x6B65;&#x9AA4;"></a>&#x4F7F;&#x7528;&#x6B65;&#x9AA4;</h3><ol>
<li><p><b style="color:red">&#x521B;&#x5EFA;&#x6A21;&#x578B;&#x6587;&#x4EF6;:</b>&#x4E00;&#x4E2A;&#x6A21;&#x578B;&#x6587;&#x4EF6;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x5E93;.<br><img src="http://upload-images.jianshu.io/upload_images/1975627-57ef108b72733513.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
</li>
<li><p><b style="color:red">&#x6DFB;&#x52A0;&#x5B9E;&#x4F53;:</b> <em>&#x4E00;&#x4E2A;&#x5B9E;&#x4F53;&#x76F8;&#x5F53;&#x4E8E;&#x4E00;&#x4E2A;&#x8868;.(&#x70B9;&#x51FB;&#x4E0B;&#x9762;add entity &#x6DFB;&#x52A0;&#x8868;, &#x70B9;&#x51FB;atttibutes&#x7684;+&#x53F7; &#x6DFB;&#x52A0;&#x5C5E;&#x6027;)</em><br><img src="http://upload-images.jianshu.io/upload_images/1975627-036a03dec180ca9f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
</li>
<li><p><b style="color:red">&#x521B;&#x5EFA;&#x5B9E;&#x4F53;&#x7C7B;:</b> &#x76F8;&#x5F53;&#x4E8E;&#x6A21;&#x578B;&#x7C7B;model.<br><img src="http://upload-images.jianshu.io/upload_images/1975627-0a11757c6d46ff45.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
</li>
<li><p><b style="color:red">&#x751F;&#x6210;&#x4E0A;&#x4E0B;&#x6587;, &#x6765;&#x5173;&#x8054;&#x6A21;&#x578B;&#x6587;&#x4EF6;&#x751F;&#x6210;&#x6570;&#x636E;&#x5E93;</b><br>&#x751F;&#x6210;&#x4E0A;&#x4E0B;&#x6587;&#x65F6;, &#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;,&#x4E14;&#x751F;&#x6210;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x6587;&#x4EF6;&#x65F6;, &#x8981;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;.</p>
</li>
<li><p><b style="color:red">&#x4ECE;&#x6570;&#x636E;&#x5E93;&#x67E5;&#x8BE2;&#x5BF9;&#x8C61;</b><br>  &#x8FC7;&#x6EE4;&#x67E5;&#x8BE2;, &#x6392;&#x5E8F;&#x67E5;&#x8BE2;, &#x5206;&#x7EC4;&#x67E5;&#x8BE2;&#x7B49;&#x7B49;</p>
</li>
<li><p><b style="color:red">&#x66F4;&#x65B0;&#x6570;&#x636E;</b><br>  &#x524D;&#x63D0;: &#x5148;&#x67E5;&#x8BE2;, &#x518D;&#x4FEE;&#x6539;&#x6570;&#x636E;</p>
</li>
<li><p><b style="color:red">&#x5220;&#x9664;&#x6570;&#x636E;</b><br>  &#x524D;&#x63D0;: &#x5148;&#x67E5;&#x8BE2;, &#x518D;&#x5220;&#x9664;&#x6570;&#x636E;</p>
</li>
<li><p><b style="color:red">&#x591A;&#x8868;&#x5173;&#x8054;</b><br>  &#x5728;relationships&#x6DFB;&#x52A0;&#x4E0E;&#x5176;&#x4ED6;&#x8868;&#x7684;&#x5173;&#x8054;<br>  &#x6CE8;&#x610F;: &#x6DFB;&#x52A0;&#x5B8C;&#x5173;&#x8054;&#x540E;, &#x8BE5;&#x8868;&#x7684;&#x5B9E;&#x4F53;&#x7C7B;&#x8981;&#x5220;&#x9664;&#x91CD;&#x65B0;&#x751F;&#x6210;<br><img src="http://upload-images.jianshu.io/upload_images/1975627-7b81372c0979bb12.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
</li>
<li><p><b style="color:red">&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;</b><br>  &#x751F;&#x6210;&#x4E0A;&#x4E0B;&#x6587;&#x65F6;, &#x5982;&#x679C;&#x6709;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;,&#x4E14;&#x751F;&#x6210;&#x591A;&#x4E2A;&#x6570;&#x636E;&#x5E93;&#x6587;&#x4EF6;&#x65F6;, &#x8981;&#x4F7F;&#x7528;&#x4E0D;&#x540C;&#x7684;&#x4E0A;&#x4E0B;&#x6587;&#x8FDB;&#x884C;&#x64CD;&#x4F5C;.</p>
</li>
</ol>
<h3 id="&#x6784;&#x6210;"><a href="#&#x6784;&#x6210;" class="headerlink" title="&#x6784;&#x6210;"></a>&#x6784;&#x6210;</h3><p>(1)<code>NSManagedObjectContext</code>&#xFF08;&#x88AB;&#x7BA1;&#x7406;&#x7684;&#x6570;&#x636E;&#x4E0A;&#x4E0B;&#x6587;&#xFF09;<br>&#x64CD;&#x4F5C;&#x5B9E;&#x9645;&#x5185;&#x5BB9;&#xFF08;&#x64CD;&#x4F5C;&#x6301;&#x4E45;&#x5C42;&#xFF09; &#x4E34;&#x65F6;&#x6570;&#x636E;&#x5E93;<br>&#x4F5C;&#x7528;&#xFF1A;&#x63D2;&#x5165;&#x6570;&#x636E;&#xFF0C;&#x67E5;&#x8BE2;&#x6570;&#x636E;&#xFF0C;&#x5220;&#x9664;&#x6570;&#x636E;</p>
<p>(2)<code>NSManagedObjectModel</code>&#xFF08;&#x88AB;&#x7BA1;&#x7406;&#x7684;&#x6570;&#x636E;&#x6A21;&#x578B;&#xFF09;<br>&#x6570;&#x636E;&#x5E93;&#x6240;&#x6709;&#x8868;&#x683C;&#x6216;&#x6570;&#x636E;&#x7ED3;&#x6784;&#xFF0C;&#x5305;&#x542B;&#x5404;&#x5B9E;&#x4F53;&#x7684;&#x5B9A;&#x4E49;&#x4FE1;&#x606F;    &#x6570;&#x636E;&#x6A21;&#x578B;<br>&#x4F5C;&#x7528;&#xFF1A;&#x6DFB;&#x52A0;&#x5B9E;&#x4F53;&#x7684;&#x5C5E;&#x6027;&#xFF0C;&#x5EFA;&#x7ACB;&#x5C5E;&#x6027;&#x4E4B;&#x95F4;&#x7684;&#x5173;&#x7CFB;<br>&#x64CD;&#x4F5C;&#x65B9;&#x6CD5;&#xFF1A;&#x89C6;&#x56FE;&#x7F16;&#x8F91;&#x5668;&#xFF0C;&#x6216;&#x4EE3;&#x7801;</p>
<p>(3)<code>NSPersistentStoreCoordinator</code>&#xFF08;&#x6301;&#x4E45;&#x5316;&#x5B58;&#x50A8;&#x52A9;&#x7406;&#xFF09;<br>&#x76F8;&#x5F53;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x7684;&#x8FDE;&#x63A5;&#x5668;<br>&#x4F5C;&#x7528;&#xFF1A;&#x8BBE;&#x7F6E;&#x6570;&#x636E;&#x5B58;&#x50A8;&#x7684;&#x540D;&#x5B57;&#xFF0C;&#x4F4D;&#x7F6E;&#xFF0C;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;&#xFF0C;&#x548C;&#x5B58;&#x50A8;&#x65F6;&#x673A;</p>
<p>(4)<code>NSManagedObject</code>&#xFF08;&#x88AB;&#x7BA1;&#x7406;&#x7684;&#x6570;&#x636E;&#x8BB0;&#x5F55;&#xFF09;<br>&#x76F8;&#x5F53;&#x4E8E;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684;&#x8868;&#x683C;&#x8BB0;&#x5F55;</p>
<p>(5)<code>NSFetchRequest</code>&#xFF08;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x7684;&#x8BF7;&#x6C42;&#xFF09;<br>&#x76F8;&#x5F53;&#x4E8E;&#x67E5;&#x8BE2;&#x8BED;&#x53E5;  &#x67E5;&#x8BE2;&#x65F6; &#x8981;&#x5EFA;&#x7ACB;&#x8BF7;&#x6C42;&#x5BF9;&#x8C61;</p>
<p>(6)<code>NSEntityDescription</code>&#xFF08;&#x5B9E;&#x4F53;&#x7ED3;&#x6784;&#xFF09;<br>&#x76F8;&#x5F53;&#x4E8E;&#x8868;&#x683C;&#x7ED3;&#x6784;</p>
<p>(7)&#x540E;&#x7F00;&#x4E3A;.xcdatamodeld&#x7684;&#x5305;<br>&#x91CC;&#x9762;&#x662F;.xcdatamodel&#x6587;&#x4EF6;&#xFF0C;&#x7528;&#x6570;&#x636E;&#x6A21;&#x578B;&#x7F16;&#x8F91;&#x5668;&#x7F16;&#x8F91; . &#x7F16;&#x8BD1;&#x540E;&#x4E3A;.momd&#x6216;.mom&#x6587;&#x4EF6;</p>
<h3 id="SQLiteManager&#x5DE5;&#x5177;"><a href="#SQLiteManager&#x5DE5;&#x5177;" class="headerlink" title="SQLiteManager&#x5DE5;&#x5177;"></a>SQLiteManager&#x5DE5;&#x5177;</h3><ul>
<li><p><code>Z_PK</code> : &#x4E3B;&#x952E; <code>ZDEPART</code>: &#x5916;&#x952E;<br><img src="http://upload-images.jianshu.io/upload_images/1975627-543672f4693585b0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
</li>
<li><p>&#x6253;&#x5F00;CoreData&#x7684;sql&#x8BED;&#x53E5;:<br>&#x6309;&#x7167;&#x4E0B;&#x56FE;&#x64CD;&#x4F5C;&#x540E;, &#x4F7F;&#x7528;CoreData&#x65F6; &#x4F1A;&#x5728;&#x63A7;&#x5236;&#x53F0;&#x8F93;&#x51FA;sql&#x8BED;&#x53E5;:<br><img src="http://upload-images.jianshu.io/upload_images/1975627-d50caf04845c5c6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""><br>&#x6DFB;&#x52A0;-com.apple.CoreData.SQLDebug &#x548C; 1 &#x4E24;&#x884C;<br>&#x6CE8;&#x610F;: &#x8FD9;&#x4E24;&#x884C;&#x7684;&#x987A;&#x5E8F;&#x5982;&#x56FE;&#x6240;&#x793A;, &#x4E0D;&#x80FD;&#x8C03;&#x6362;.<br><img src="http://upload-images.jianshu.io/upload_images/1975627-8c49644ef29bb95f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt=""></p>
</li>
</ul>
<h3 id="SQLite-&#x548C;-CoreData-&#x7684;&#x5BF9;&#x6BD4;"><a href="#SQLite-&#x548C;-CoreData-&#x7684;&#x5BF9;&#x6BD4;" class="headerlink" title="SQLite &#x548C; CoreData &#x7684;&#x5BF9;&#x6BD4;"></a>SQLite &#x548C; CoreData &#x7684;&#x5BF9;&#x6BD4;</h3><blockquote>
<ol>
<li>sqlite&#x662F;&#x7EAF;C&#x8BED;&#x8A00;&#x5B58;&#x50A8;&#x65B9;&#x5F0F;.</li>
<li>CoreData&#x662F;&#x5BF9;sqlite&#x7684;&#x5C01;&#x88C5;, &#x5B83;&#x7684;&#x6570;&#x636E;&#x7684;&#x4FDD;&#x5B58;&#x76F4;&#x63A5;&#x4F7F;&#x7528;&#x5BF9;&#x8C61;, &#x4E0D;&#x7528;&#x518D;&#x5199;sql&#x8BED;&#x53E5;.</li>
<li>&#x6027;&#x80FD;&#x65B9;&#x9762;: sqlite &#x4F18;&#x4E8E; CoreData</li>
<li>&#x4EC0;&#x4E48;&#x65F6;&#x5019;&#x4F7F;&#x7528;sqlite &#x548C; CoreData?<ul>
<li>&#x5982;&#x679C;&#x6570;&#x636E;&#x5E93;&#x8868;&#x5173;&#x8054;&#x6BD4;&#x8F83;&#x590D;&#x6742;&#x65F6;, &#x4E09;&#x56DB;&#x5F20;&#x8868;&#x90FD;&#x5173;&#x8054;, &#x90A3;&#x5C31;&#x4F7F;&#x7528;sqlite</li>
<li>&#x5982;&#x679C;&#x6570;&#x636E;&#x5E93;&#x8868;&#x7ED3;&#x6784;&#x6BD4;&#x8F83;&#x7B80;&#x5355;, &#x8868;&#x4E4B;&#x95F4;&#x6CA1;&#x6709;&#x5173;&#x7CFB;, &#x4F7F;&#x7528;CoreData</li>
</ul>
</li>
</ol>
</blockquote>

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/数据存储/" rel="tag">#数据存储</a>
          
        </div>
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/posts/2016-11/01-1.html" rel="next" title="iOS开发 - 关于 pch 文件的创建与配置">
                <i class="fa fa-chevron-left"></i> iOS开发 - 关于 pch 文件的创建与配置
              </a>
            
          </div>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/posts/2016-11/03-4.html" rel="prev" title="iOS开发 - 数据存储 NSUserDefaults 的使用">
                iOS开发 - 数据存储 NSUserDefaults 的使用 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>





    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
      <div class="ds-thread" data-thread-key="posts/2016-11/03-2.html"
           data-title="iOS开发 - 数据存储 CoreData 的使用" data-url="http://www.licheng244.com/posts/2016-11/03-2.html">
      </div>
    

        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel ">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="//schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/uploads/avatar.jpg"
               alt="Li Cheng" />
          <p class="site-author-name" itemprop="name">Li Cheng</p>
          <p class="site-description motion-element" itemprop="description">| 写写代码 | 做做设计 | <br>记录着工作和学习中的点点滴滴 ~~~</p>
        </div>
        <nav class="site-state motion-element">
          <div class="site-state-item site-state-posts">
            <a href="/archives">
              <span class="site-state-item-count">17</span>
              <span class="site-state-item-name">日志</span>
            </a>
          </div>

          
            <div class="site-state-item site-state-categories">
              <a href="/categories">
                <span class="site-state-item-count">8</span>
                <span class="site-state-item-name">分类</span>
              </a>
            </div>
          

          
            <div class="site-state-item site-state-tags">
              <a href="/tags">
                <span class="site-state-item-count">13</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
        </div>

        
        

        
        

      </section>

      
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">
            
              
            
            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#工作原理"><span class="nav-number">1.</span> <span class="nav-text">工作原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#使用步骤"><span class="nav-number">2.</span> <span class="nav-text">使用步骤</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#构成"><span class="nav-number">3.</span> <span class="nav-text">构成</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SQLiteManager工具"><span class="nav-number">4.</span> <span class="nav-text">SQLiteManager工具</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SQLite-和-CoreData-的对比"><span class="nav-number">5.</span> <span class="nav-text">SQLite 和 CoreData 的对比</span></a></li></ol></div>
            
          </div>
        </section>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Li Cheng</span>
</div>

<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>

        

        
      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  



  
  <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>

  
  <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>

  
  <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

  
  <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.0.2"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.0.2"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.0.2"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.0.2"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.0.2"></script>



  

  
    
  

  <script type="text/javascript">
    var duoshuoQuery = {short_name:"licheng244344094"};
    (function() {
      var ds = document.createElement('script');
      ds.type = 'text/javascript';ds.async = true;
      ds.id = 'duoshuo-script';
      ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
      ds.charset = 'UTF-8';
      (document.getElementsByTagName('head')[0]
      || document.getElementsByTagName('body')[0]).appendChild(ds);
    })();
  </script>

  
    
    <script src="/lib/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>
    <script src="/js/src/hook-duoshuo.js"></script>
  






  
  
  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length == 0) {
       search_path = "search.xml";
    }
    var path = "/" + search_path;
    // monitor main search box;

    function proceedsearch() {
      $("body").append('<div class="popoverlay">').css('overflow', 'hidden');
      $('.popup').toggle();

    }
    // search function;
    var searchFunc = function(path, search_id, content_id) {
    'use strict';
    $.ajax({
        url: path,
        dataType: "xml",
        async: true,
        success: function( xmlResponse ) {
            // get the contents from search data
            isfetched = true;
            $('.popup').detach().appendTo('.header-inner');
            var datas = $( "entry", xmlResponse ).map(function() {
                return {
                    title: $( "title", this ).text(),
                    content: $("content",this).text(),
                    url: $( "url" , this).text()
                };
            }).get();
            var $input = document.getElementById(search_id);
            var $resultContent = document.getElementById(content_id);
            $input.addEventListener('input', function(){
                var matchcounts = 0;
                var str='<ul class=\"search-result-list\">';
                var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                $resultContent.innerHTML = "";
                if (this.value.trim().length > 1) {
                // perform local searching
                datas.forEach(function(data) {
                    var isMatch = false;
                    var content_index = [];
                    var data_title = data.title.trim().toLowerCase();
                    var data_content = data.content.trim().replace(/<[^>]+>/g,"").toLowerCase();
                    var data_url = decodeURIComponent(data.url);
                    var index_title = -1;
                    var index_content = -1;
                    var first_occur = -1;
                    // only match artiles with not empty titles and contents
                    if(data_title != '') {
                        keywords.forEach(function(keyword, i) {
                            index_title = data_title.indexOf(keyword);
                            index_content = data_content.indexOf(keyword);
                            if( index_title >= 0 || index_content >= 0 ){
                                isMatch = true;
								if (i == 0) {
                                    first_occur = index_content;
                                }
                            } 
							
                        });
                    }
                    // show search results
                    if (isMatch) {
                        matchcounts += 1;
                        str += "<li><a href='"+ data_url +"' class='search-result-title'>"+ data_title +"</a>";
                        var content = data.content.trim().replace(/<[^>]+>/g,"");
                        if (first_occur >= 0) {
                            // cut out 100 characters
                            var start = first_occur - 20;
                            var end = first_occur + 80;
                            if(start < 0){
                                start = 0;
                            }
                            if(start == 0){
                                end = 50;
                            }
                            if(end > content.length){
                                end = content.length;
                            }
                            var match_content = content.substring(start, end);
                            // highlight all keywords
                            keywords.forEach(function(keyword){
                                var regS = new RegExp(keyword, "gi");
                                match_content = match_content.replace(regS, "<b class=\"search-keyword\">"+keyword+"</b>");
                            });

                            str += "<p class=\"search-result\">" + match_content +"...</p>"
                        }
                        str += "</li>";
                    }
                })};
                str += "</ul>";
                if (matchcounts == 0) { str = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>' }
                if (keywords == "") { str = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>' }
                $resultContent.innerHTML = str;
            });
            proceedsearch();
        }
    });}

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched == false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };

    });

    $('.popup-btn-close').click(function(e){
      $('.popup').hide();
      $(".popoverlay").remove();
      $('body').css('overflow', '');
    });
    $('.popup').click(function(e){
      e.stopPropagation();
    });
  </script>


  

  

  
  <script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
  <script>AV.initialize("SJtE0CaYCSQVrkVXmWMYWJOd-gzGzoHsz", "TfVyU54NEToMYg0SId3KELgz");</script>
  <script>
    function showTime(Counter) {
      var query = new AV.Query(Counter);
      var entries = [];
      var $visitors = $(".leancloud_visitors");

      $visitors.each(function () {
        entries.push( $(this).attr("id").trim() );
      });

      query.containedIn('url', entries);
      query.find()
        .done(function (results) {
          var COUNT_CONTAINER_REF = '.leancloud-visitors-count';

          if (results.length === 0) {
            $visitors.find(COUNT_CONTAINER_REF).text(0);
            return;
          }

          for (var i = 0; i < results.length; i++) {
            var item = results[i];
            var url = item.get('url');
            var time = item.get('time');
            var element = document.getElementById(url);

            $(element).find(COUNT_CONTAINER_REF).text(time);
          }
          for(var i = 0; i < entries.length; i++) {
            var url = entries[i];
            var element = document.getElementById(url);
            var countSpan = $(element).find(COUNT_CONTAINER_REF);
            if( countSpan.text() == '') {
              countSpan.text(0);
            }
          }
        })
        .fail(function (object, error) {
          console.log("Error: " + error.code + " " + error.message);
        });
    }

    function addCount(Counter) {
      var $visitors = $(".leancloud_visitors");
      var url = $visitors.attr('id').trim();
      var title = $visitors.attr('data-flag-title').trim();
      var query = new AV.Query(Counter);

      query.equalTo("url", url);
      query.find({
        success: function(results) {
          if (results.length > 0) {
            var counter = results[0];
            counter.fetchWhenSave(true);
            counter.increment("time");
            counter.save(null, {
              success: function(counter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(counter.get('time'));
              },
              error: function(counter, error) {
                console.log('Failed to save Visitor num, with error message: ' + error.message);
              }
            });
          } else {
            var newcounter = new Counter();
            /* Set ACL */
            var acl = new AV.ACL();
            acl.setPublicReadAccess(true);
            acl.setPublicWriteAccess(true);
            newcounter.setACL(acl);
            /* End Set ACL */
            newcounter.set("title", title);
            newcounter.set("url", url);
            newcounter.set("time", 1);
            newcounter.save(null, {
              success: function(newcounter) {
                var $element = $(document.getElementById(url));
                $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
              },
              error: function(newcounter, error) {
                console.log('Failed to create');
              }
            });
          }
        },
        error: function(error) {
          console.log('Error:' + error.code + " " + error.message);
        }
      });
    }

    $(function() {
      var Counter = AV.Object.extend("Counter");
      if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
      } else if ($('.post-title-link').length > 1) {
        showTime(Counter);
      }
    });
  </script>



  

  


</body>
</html>
